/*************************************************************************
	> File Name: acquisitor.h
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年02月02日 星期五 14时22分48秒
 ************************************************************************/
#ifndef __ACQUISITOR_H__
#define __ACQUISITOR_H__

#include <stdio.h>
#include <utils/timer.h>
#include <utils/shared_ptr.h>
#include <transport/transport.h>
#include <utils/sqlite.h>
#include <string>
#include "template.h"
#include <utils/fixlist.cpp>
#include "acqdata.h"
#include "acqitem.h"
#include "data.h"

class Acquisitor:public JsonRpcInterface
{
public:
    Acquisitor(const deviceinfo_t& devinfo);
    virtual ~Acquisitor();

    //采集数据
    virtual void Run(
            tConnection connector,
            const time_t &acqtime,
            vector<AcqItem> &listAcquire) = 0;

    virtual void DoRun(
            tConnection connector,
            const time_t &acqtime);

    BOOL IsEqualId(S32 id);
    S32 DevId();

    OpValue indicateDevice(
            tConnection connector,
            const string &itemId,
            const vector<OpValue> &params);

    OpValue indicateDevice(
            tConnection connector,
            S32 itemId,
            const vector<OpValue> &params);

    OpValue indicateDevice(
            tConnection connector,
            const string &itemId,
            const OpValue &param = OpValue::Null());

    OpValue indicateDevice(
            tConnection connector,
            S32 itemId,
            const OpValue &param = OpValue::Null());

//    U16 acquireDevice(
//            tConnection connector,
//            U16 index, U16 len,
//            vector<OpValue> &out);

    Json::Value acquireList();

    U16 acquireDevice(
            tConnection connector,
            tDataItem &item,
            OpValue &result,
            vector<CmdValue> *listResult = NULL);

    //远程提交
    virtual S32 commitData(int index) = 0;
    virtual void clearData();
    virtual S32 sendRpc(int index,
            const string &method,
            const Json::Value &params);
    void commitError(const string &err);

    tDataItem GetItemByIndex(S32 index);
    tDataItem GetItemById(S32 itemId);
    tDataItem GetItemByDot(const string &dot);

    //flag: 3-采集一般数据 1-所有状态
    tDataItem GetAcqItem(tDataItem item, U8 flag);

    virtual BOOL OnReceiveJson(
            S32 index,
            DAS_RESULT result,
            const string &method, const Json::Value &json);

    bool acquireStatus();
    bool powerStatus(tConnection &connector);

    //更新
    void updateAcquisitor(const deviceinfo_t& devinfo);
    BOOL updateAquireData(const deviceinfo_t &devinfo);
    void startAcquire();
    void stopAcquire();
    bool isStop();

    int acqCount();

    string dateType(tDataItem &item);
protected:
    deviceinfo_t     m_idevinfo;
    Lock                    m_iLock;
    std::vector<tDataItem>   m_listDataItems;

    bool                    m_bStop;
    bool                    m_bStat;

    vector<AcqItem>  listAcquire;
};

#endif //__ACQUISITOR_H__

